﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BLL
{
    public class Lc_BlockBLL : BaseBLL
    {
        public List<Lc_Block> GetByID(string ids)
        {
            var data = new List<Lc_Block>();
            if (string.IsNullOrEmpty(ids))
            {
                return data;
            }
            var sql = string.Format("SELECT N_BLOCK_ID,C_BLOCK_NAME,C_BLOCK_DEPATH,REMARK FROM LC_BLOCK WHERE N_BLOCK_ID IN ({0})", ids);
            var dt = base.Query(sql, "", "", "", "");
            foreach (DataRow item in dt.Rows)
            {
                var entity = new Lc_Block
                {
                    N_BLOCK_ID = Convert.ToDecimal(item["N_BLOCK_ID"]),
                    C_BLOCK_NAME = Convert.ToString(item["C_BLOCK_NAME"]),
                    C_BLOCK_DEPATH = Convert.ToString(item["C_BLOCK_DEPATH"]),
                    REMARK = Convert.ToString(item["REMARK"])
                };
                data.Add(entity);
            }
            return data;
        }

        public List<Lc_Block> GetByID(string[] idarry)
        {
            var ids = "";
            foreach (var item in idarry)
            {
                ids += item + ",";
            }
            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                return GetByID(ids);
            }
            else
            {
                return new List<Lc_Block>();
            }
        }

        public Lc_Block GetEntity(decimal id)
        {
            var sql = string.Format("SELECT N_BLOCK_ID,C_BLOCK_NAME,C_BLOCK_DEPATH,REMARK FROM LC_BLOCK WHERE N_BLOCK_ID = {0}", id);

            var data = base.Query(sql, "", "", "", "");
            if (data.Rows.Count > 0)
            {
                var entity = new Lc_Block
                {
                    N_BLOCK_ID = Convert.ToDecimal(data.Rows[0]["N_BLOCK_ID"]),
                    C_BLOCK_NAME = Convert.ToString(data.Rows[0]["C_BLOCK_NAME"]),
                    C_BLOCK_DEPATH = Convert.ToString(data.Rows[0]["C_BLOCK_DEPATH"]),
                    REMARK = Convert.ToString(data.Rows[0]["REMARK"])
                };
                return entity;
            }
            return null;
        }

        public DataTable GetByDataTable(string ids)
        {
            var sql = "SELECT N_BLOCK_ID,C_BLOCK_NAME,C_BLOCK_DEPATH,REMARK FROM LC_BLOCK WHERE N_BLOCK_ID IN(" + ids + ")";
            return base.Query(sql, "", "", "", "");
        }

        public DataTable GetAllInfo(string Style, object param)
        {
            string sql = string.Format(@"SELECT
    lcb.N_BLOCK_ID,
    lcb.C_BLOCK_NAME,
    lcb.C_BLOCK_DEPATH,
    lcb.REMARK
FROM
    Lc_Block lcb
WHERE
    1=1");
            if (Style == "BlockName")
            {
                sql += " AND lcb.C_BLOCK_NAME='" + param + "'";
            }
            else if (Style == "Key")
            {
                sql += " AND lcb.C_BLOCK_NAME like '%" + param + "%'";
            }
            else if (Style == "BlockID")
            {
                sql += " AND lcb.N_BLOCK_ID IN(" + param + ")";
            }

            sql += " ORDER BY lcb.C_BLOCK_NAME ";
            return this.Query(sql, "", "", "", "");
        }

        public int INSERT(Lc_Block model, string operName)
        {
            model.N_BLOCK_ID = GetSequence("N_BLOCK_ID");
            string sql = string.Format(@"INSERT INTO LC_BLOCK(N_BLOCK_ID,C_BLOCK_NAME,C_BLOCK_DEPATH,REMARK) VALUES({0},'{1}','{2}','{3}')", model.N_BLOCK_ID, model.C_BLOCK_NAME, model.C_BLOCK_DEPATH, model.REMARK);
            return base.NoQuery(sql, operName);
        }

        public int UPDATE(Lc_Block model, string operName)
        {
            string sql = string.Format(@"UPDATE LC_BLOCK SET
C_BLOCK_NAME = '{0}',
C_BLOCK_DEPATH = '{1}',
REMARK = '{2}'
WHERE N_BLOCK_ID = '{3}'
", model.C_BLOCK_NAME, model.C_BLOCK_DEPATH, model.REMARK, model.N_BLOCK_ID);
            return base.NoQuery(sql, operName);
        }

        public int DELETE(string ids, string operName)
        {
            if (string.IsNullOrEmpty(ids))
            {
                return 1;
            }

            string sql = string.Format(@"DELETE FROM LC_BLOCK WHERE N_BLOCK_ID IN(" + ids + ")");
            return base.NoQuery(sql, operName);
        }

        /// <summary>
        /// 获取行数m
        /// </summary>
        /// <param name="Style"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public int GetRecordCount(string Style, object param)
        {
            string sql = string.Format(@"SELECT count (*) FROM THKQS.Lc_Block lcb WHERE 1 = 1 ");
            if (Style == "BlockName")
            {
                sql += " AND lcb.C_BLOCK_NAME='" + param + "'";
            }
            else if (Style == "Key")
            {
                sql += " AND lcb.C_BLOCK_NAME like '%" + param + "%'";
            }
            else if (Style == "BlockID")
            {
                sql += " AND lcb.N_BLOCK_ID IN(" + param + ")";
            }
            try
            {
                return Convert.ToInt32(this.Query(sql, "", "", "", "").Rows[0][0]);
            }
            catch (Exception)
            {
                throw new Exception("LC_BLOCK-GetRecordCount-ERROR");
            }
        }

        public DataTable GetListByPage(string Style, object param, string orderby, int startIndex, int endIndex)
        {
            string sql = string.Format(@"SELECT * FROM (SELECT  ROW_NUMBER() OVER(ORDER BY lcb." + orderby + @") as r,
    lcb.N_BLOCK_ID,
    lcb.C_BLOCK_NAME,
    lcb.C_BLOCK_DEPATH,
    lcb.REMARK
FROM
    Lc_Block lcb
WHERE
    1=1 ");
            if (Style == "BlockName")
            {
                sql += " AND lcb.C_BLOCK_NAME='" + param + "'";
            }
            else if (Style == "Key")
            {
                sql += " AND lcb.C_BLOCK_NAME like '%" + param + "%'";
            }
            else if (Style == "BlockID")
            {
                sql += " AND lcb.N_BLOCK_ID IN(" + param + ")";
            }
            sql += @" ) TT";

            if (startIndex >= 2)
            {
                sql += " WHERE TT.r BETWEEN (" + startIndex + "-1)*" + endIndex + "+1 AND " + startIndex + "*" + endIndex + "";
            }
            else
            {
                sql += " WHERE TT.r BETWEEN (" + startIndex + "-1)*" + endIndex + " AND " + startIndex + "*" + endIndex + "";
            }
            return Query(sql);
        }

        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <param name="whereStr"></param>
        /// <param name="orderbyStr"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public DataTable GetDataByPage(int skip, int take, string whereStr, string orderbyStr, out int count)
        {
            var sqlBuilder = new StringBuilder();
            sqlBuilder.AppendLine("SELECT * FROM LC_BLOCK");
            if (whereStr != null)
            {
                sqlBuilder.AppendFormat(" WHERE {0}", whereStr);
            }
            if (orderbyStr != null)
            {
                sqlBuilder.AppendFormat(" ORDER BY {0}", orderbyStr);
            }
            var sql = sqlBuilder.ToString();
            count = base.SqlCount(sql);
            return base.PageQuery(sql, skip, take);
        }

        public List<Lc_Block> GetEntityByPage(int skip, int take, string whereStr, string orderbyStr, out int count)
        {
            var dt = GetDataByPage(skip, take, whereStr, orderbyStr, out count);
            var data = new List<Lc_Block>();
            foreach (DataRow item in dt.Rows)
            {
                var entity = new Lc_Block
                {
                    N_BLOCK_ID = Convert.ToDecimal(item["N_BLOCK_ID"]),
                    C_BLOCK_DEPATH = Convert.ToString(item["C_BLOCK_DEPATH"]),
                    C_BLOCK_NAME = Convert.ToString(item["C_BLOCK_NAME"]),
                    REMARK = Convert.ToString(item["REMARK"])
                };
                data.Add(entity);
            }
            return data;
        }
    }
}